package org.unt.parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.unt.genlogic.Files;

public class Parser {

	public static void main(String[] args) throws IOException {
		new Parser().process();
	}

	void process() throws IOException {

		List lines = Files.readFileAsList("C:/Speeder/Jira Evols/447. New help/aides en lignes speeder/AIDE_EN_LIGNE_ES.htm");

		StringBuffer buf = new StringBuffer();
		List output = new ArrayList();

		for (Iterator iterator = lines.iterator(); iterator.hasNext();) {

			String line = (String) iterator.next();
			buf.append(line.trim() + " ");
		}

		String clave = null;
		String ppp = null;
		String text = null;

		int i = 1;
		while (buf.indexOf("<td") > 0) {

			int from = buf.indexOf("<td");
			int to = buf.indexOf("</td>");

			if ((i - 4) % 6 == 0 || (i - 5) % 6 == 0 || (i - 6) % 6 == 0) {

				String content = buf.substring(from, to);

				content = content.substring(content.indexOf(">") + 1).trim();

				if ((i - 4) % 6 == 0) {

					clave = content;

				} else if ((i - 5) % 6 == 0) {

					ppp = content;

				} else if ((i - 6) % 6 == 0) {

					text = content;

					output.add(new Tri(clave, ppp, text)); // IMPORTANTE
				}
			}

			buf.delete(0, to + 5);
			i++;
		}

		int j = 1;
		for (Iterator iterator = output.iterator(); iterator.hasNext();) {

			Tri tri = (Tri) iterator.next();

			Files.writeToFile(tri.getText(), "C:/Speeder/Jira Evols/447. New help/aides en lignes speeder/Rafa/aideLigne" + j++ + ".html");
		}
	}

	public class Tri {

		private String clave;

		private String ppp;

		private String text;

		public Tri(String clave, String ppp, String text) {
			super();
			this.clave = clave;
			this.ppp = ppp;
			this.text = text;
		}

		public String getClave() {
			return clave;
		}

		public void setClave(String clave) {
			this.clave = clave;
		}

		public String getPpp() {
			return ppp;
		}

		public void setPpp(String ppp) {
			this.ppp = ppp;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}
	}
}
